package kuzmenkov.oip.adapters.nih.imagej;

import ij.ImagePlus;
import ij.io.FileSaver;
import ij.process.ImageProcessor;
import kuzmenkov.oip.adapters.ImageProcessingAdapter;
import kuzmenkov.oip.common.FileTool;

import java.util.logging.Logger;

/**
 * User: Michael Kuzmenkov
 * Date: 16.04.2009
 * Time: 0:31:53
 */
public class NoiseGeneratorAdapter extends ImageProcessingAdapter {
    private Logger log = Logger.getLogger(NoiseGeneratorAdapter.class.getName());

    /**
     * Adds random noise (random numbers within range) to the image.
     * It requires two parameters:
     * the source image [in] (java.lang.String)
     * the range [in] (java.lang.Float)
     * It returns a path to a noised image (java.lang.String)
     *
     * @param params the input parameters
     * @return the path to the noised image
     * @throws Exception
     */
    public Object[] process(Object[] params) throws Exception {
        String path = (String) params[0];
        double range = (Float) params[1];

        log.info("path=" + path + ", range=" + range);

        FileTool ftool = FileTool.getInstance();
        path = ftool.relPathToAbs(path);
        log.info("modified path="+path);

        ImagePlus srcImage = new ImagePlus(path);
        ImageProcessor processor = srcImage.getProcessor();
        processor.noise(range);

        ImagePlus dstImage = srcImage;

        
        String dstFileName = "1.png";
        String dstPath = ftool.getTempDir(getSessionId())+dstFileName;
        log.info("destination path="+dstPath);

        FileSaver saver = new FileSaver(dstImage);
        saver.saveAsPng(dstPath);

        String relDstPath = ftool.getRelativeTempDir(getSessionId())+dstFileName;
        log.info("relative destination path="+relDstPath);
        return new Object[]{relDstPath};
        
    }
}
